package pl.enigmatic.parser.xml;

import java.lang.reflect.Method;
import java.util.Map;

import pl.enigmatic.util.Reflection;


public class SimplePropertyGetter implements PropertyGetter {

	protected final Parser<?> owner;
	private final Map<String, Method> getters;

	public SimplePropertyGetter(Parser<?> parser) {
		this.owner = parser;
		getters = Reflection.getDeclaredGetters(owner.getClazz());
	}

	@Override
	public Class<?> getPropertyType(String property) {
		if (getters.containsKey(property)) {
			return getters.get(property).getReturnType();
		} else {
			return null;
		}
	}

	@Override
	public Object getPropertyValue(Object target, String property) {
		try {
			return getters.get(property).invoke(target);
		} catch (Exception e) {
			return null;
		}
	}

}
